// subPages/communityService/mySecondHandMall/mySecondHandMall.js
const app = getApp();
var utils = require('../../../utils/util');
var loadMoreView,p = 1,num = 4,type='goods'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor:app.globalData.themeColor,
      goodsList:[],

  },
    editBtn(e){
      var id= e.currentTarget.dataset.id;
      wx.navigateTo({
          url:'/subPages/communityService/upLoadGoods/upLoadGoods?type=2&id='+id
      })
    },
    gotoDetails(e){
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/subPages/communityService/secondHandMallDetails/secondHandMallDetails?id='+id
        })
    },
    upProduct(e){
        let id = e.currentTarget.dataset.id;
        let that = this;
        let pro = {
            id:id,
            status:10
        }
        utils.getAjax(`${app.globalData.apiUrl}/add_old_product`,res=>{
            if(res.data == 1){
                wx.showModal({
                    title:'提示',
                    content:'您已成功上架商品！',
                    showCancel:false,
                    success(res){
                        if(res.confirm){
                           that.getMyList();
                        }
                    }
                })
            }
        },{
            pro:pro,
            type:2
        })
    },
    downProduct(e){
        let id = e.currentTarget.dataset.id;
        let that = this;
        let pro = {
            id:id,
            status:1
        }
        utils.getAjax(`${app.globalData.apiUrl}/add_old_product`,res=>{
            if(res.data == 1){
                wx.showModal({
                    title:'提示',
                    content:'您已成功下架商品！',
                    showCancel:false,
                    success(res){
                        if(res.confirm){
                            that.getMyList();
                        }
                    }
                })
            }
        },{
            pro:pro,
            type:2
        })
    },
    delMySecondHand(e){
      var goodsId = e.currentTarget.dataset.id;
      var _this = this;
      wx.showModal({
          title:'提示',
          content:'您是否要删除该商品？',
          success(res){
              if(res.confirm){
                utils.getAjax(`${app.globalData.apiUrl}/delete_old_product`,res=>{
                    console.log(res);
                    if(res.data == 1){
                        utils.showToast('商品删除成功！','none',2000);
                        wx.pageScrollTo({
                            scrollTop: 0
                        })
                        p=1;
                        _this.getMyList();
                    }
                },{
                    id:goodsId
                })
              }
              if(res.cancel){
                  return
              }
          }

      })
    },
    getMyList(){
        var that = this;
        utils.getAjax(`${app.globalData.apiUrl}/get_user_old_product`,function(res){
            console.log(res);
            if(res.data.list.length != 0){
                var list = [];
                if(p == 1){
                    list = res.data.list
                }else{
                    list = that.data.goodsList.concat(res.data.list)
                }
                that.setData({
                    goodsList:list
                })
            }else{
                that.setData({
                    goodsList:[]
                })
            }
            loadMoreView.loadMoreComplete(res.data,type)
        },{
            user_id:app.globalData.userId,
            p:p,
            num:num
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      p=1;
      utils.setNavBarTitle('我的二手商城');
      loadMoreView = this.selectComponent("#loadMoreView")
      this.getMyList();

  },
    toUpLoadGoods(){
      wx.navigateTo({
          url:'/subPages/communityService/upLoadGoods/upLoadGoods?type=1'
      })
    },
    // 获取服务数据异常
    shopDataErr(err){
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            util.showModal('提示','获取商品失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },
    loadMoreListener: function(e) {
        p += 1
        this.getMyList()
    },

    clickLoadMore: function(e) {
        this.getMyList()
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      wx.pageScrollTo({
          scrollTop: 0
      })
      p = 1;
      this.getMyList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.isFail){
          this.getMyList()
          return

      }
      loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
